<template>
  <div class="employees">
    <div class="class">
      <pagetools>
        <template #after>
          <el-button type="primary" size="mini" @click="daoru">导入</el-button>
          <el-button type="primary" size="mini" @click="addClick"><i class="el-icon-plus" />新增员工</el-button>
        </template>
      </pagetools>
    </div>
    <el-card class="bottom">
      <el-table :data="list">
        <el-table-column label="序号" type="index" />
        <el-table-column label="头像">
          <template slot-scope="{row}">
            <img v-setimg="defaultimg" class="myimg" :src="row.staffPhoto" alt="" @click="showImg(row.staffPhoto)">
          </template>
        </el-table-column>
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="workNumber" label="工号" />
        <el-table-column label="聘用形式">
          <template slot-scope="{row}">
            {{ row.formOfEmployment|myemplyees }}
          </template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门" />
        <el-table-column label="入职时间">
          <template slot-scope="{row}">
            {{ row.timeOfEntry|timefilter }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="400">
          <template slot-scope="{row}">
            <el-button type="text" @click="views(row.id)">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="assignRoles(row.id)">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChange"
        @current-change="currentChange"
      />
    </el-card>
    <!-- 二维码弹出层 -->
    <el-dialog title="图片二维码" :visible="showcode" @close="showcode=false">
      <canvas ref="myCanvas" />
    </el-dialog>
    <!-- 分配角色弹出框 -->
    <el-dialog title="分配角色" :visible.sync="showRoles" @close="cancel">
      <el-checkbox-group v-model="check">
        <el-checkbox v-for="(item,index) in checkList" :key="index" :label="item.id" @click="checks">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <addpanel :showdialog.sync="showdialog" @getemplyeesInfo="getemplyeesInfo" />
  </div>
</template>

<script>
import { employeesInfo, employeesjibenInfo, setRoleApi } from '@/api/employees.js'
import { getAllRoleApi } from '@/api/role'
import QrCode from 'qrcode'
export default {
  components: {
    addpanel: () => import ('./components/add.vue')
  },
  data() {
    return {
      currentPage: 1,
      pageSizes: [5, 10, 15, 20],
      pageSize: 10,
      total: 0,
      list: [],
      showdialog: false,
      defaultimg: require('@/assets/common/bigUserHeader.png'),
      // 二维码弹出框显示
      showcode: false,
      // 分配角色弹出框显示
      showRoles: false,
      // 分配角色列表
      checkList: [],
      // el-checkbox数据
      check: [],
      // 角色id
      id: ''
    }
  },
  created() {
    this.getemplyeesInfo()
    this.getAllRole()
  },
  methods: {
    checks() {
      console.log(this.checkList)
      console.log(this.check)
    },
    // 分配角色显示
    async assignRoles(id) {
      this.showRoles = true
      console.log(id)
      this.id = id
      try {
        const res = await employeesjibenInfo(id)
        console.log(res)
        this.check = res.roleIds
      } catch {
        console.log('出错')
      }
    },
    // 得到所有的角色信息
    async getAllRole() {
      const res = await getAllRoleApi()
      console.log(res)
      this.checkList = res.rows
    },
    // 分配角色提交
    async submit() {
      try {
        const res = await setRoleApi({
          id: this.id,
          roleIds: this.check
        })
        console.log(res)
        this.$message.success('修改角色成功')
        // 关闭面板
        this.showRoles = false
      } catch {
        console.log('出错了')
      }
    },
    // 分配角色框隐藏
    cancel() {
      this.showRoles = false
    },
    // 点击头像显示二维码弹出框
    showImg(item) {
      // 判断item是否存在
      if (item) {
        this.showcode = true
        this.$nextTick(() => {
          QrCode.toCanvas(this.$refs.myCanvas, item, {
            width: 200,
            height: 200
          })
        })
      } else {
        this.$message.error('暂无图片')
      }
    },
    // 查看跳转到员工信息页面detail
    views(row) {
      this.$router.push(`/employees/detail?id=${row}`)
    },
    // 导入事件
    daoru() {
      this.$router.push('/employees/excel')
    },
    // 打开新增面板
    addClick() {
      this.showdialog = true
    },
    // 获得员工列表
    async getemplyeesInfo() {
      const res = await employeesInfo({
        page: this.currentPage,
        size: this.pageSize
      })
      console.log(res)
      this.list = res.rows
      this.total = res.total
    },
    // 页容量发生改变
    sizeChange(size) {
      this.pageSize = size
      this.getemplyeesInfo()
    },
    // 当前页发生改变
    currentChange(page) {
      this.currentPage = page
      this.getemplyeesInfo()
    }
  }
}
</script>

<style lang="scss">
  .employees{
   // 下方卡片结构
   .bottom{
margin-top: 20px;
  .myimg{
    width: 30px;
    height: 30px;
    img{
      width: 100%;
      height: 100%;
    }
  }
   }
   .el-pagination{
     text-align: right;
   }
  }
</style>
